package deck;

import java.util.List;
import java.util.Random;
import java.util.Vector;

public class Deck {
	private List<Card> deck;
	
	public Deck() {
		deck = new Vector<Card>();
		replenish();
	}
	
	public void replenish() {
		deck.clear();
		for (Suit s : Suit.values()) {
			for (Value v : Value.values()) {
				deck.add(new Card(s, v));
			}
		}
	}
	
	public void shuffle() {
		Random rnd = new Random();
		for (int i = 0; i < rnd.nextInt(5) + 1; i++) {
			shuffleOnce();
		}
	}
	
	private void shuffleOnce() {
		Random rnd = new Random();
		for (int i = 0; i < deck.size(); i++) {
			int swapIndex = rnd.nextInt(52);
			Card tmp = deck.remove(swapIndex);
			if (swapIndex == 51) {
				deck.add(deck.remove(0));
			} else {
				deck.add(swapIndex, deck.remove(0));
			}
			deck.add(0, tmp);
		}
	}
	public void debugPrint() {
		for (int i = 0; i < deck.size(); i++) {
			System.out.println(deck.get(i));
		}
	}
	
	public List<Card> drawCardsFromDeck(int numberOfCards) throws OutOfCardsException {
		if (numberOfCards > deck.size()) {
			throw new OutOfCardsException();
		}
		List<Card> topCards = new Vector<Card>();
		for (int i = 0; i < numberOfCards; i++) {
			topCards.add(deck.remove(0));
		}
		return topCards;
	}
}
